package com.atguigu.gmall.mq.config;

import com.rabbitmq.client.AMQP;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Configuration // 用配置文件形式去绑定.
@Component
public class DeadLetterMqConfig {

    //  声明变量：
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";
    //  声明队列
    @Bean
    public Queue queue1(){
        //  声明一个map 集合
        HashMap<String, Object> map = new HashMap<>();
        //  设置消息转发的地址.
        map.put("x-dead-letter-exchange",exchange_dead);
        map.put("x-dead-letter-routing-key",routing_dead_2);
        //  设置过期时间. 10秒 数据类型是int ,不能是字符串.
        map.put("x-message-ttl",10000);
        return new Queue(queue_dead_1,true,false,false,map);
    }

    //  声明交换机
    @Bean
    public DirectExchange exchange(){
        //  返回交换机.
        return new DirectExchange(exchange_dead,true,false,null);
    }

    //  设置绑定关系.
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);
    }

    //  声明队列2
    @Bean
    public Queue queue2(){
        return new Queue(queue_dead_2,true,false,false);
    }

    //  设置绑定关系.
    @Bean
    public Binding binding2(){
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }

}
